package org.soria.getinfo.utils;

import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.GraphicsCard;
import oshi.hardware.HWDiskStore;
import oshi.hardware.PhysicalMemory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class InfoUtil {
    private static final SystemInfo systemInfo = new SystemInfo();
    // 操作系统
    public static String OPERATE_SYSTEM;
    // CPU信息
    public static Map<String, String> CPU;
    // GPU信息
    public static Map<Integer, Map<String, String>> GPU;
    // 内存信息
    public static Map<Integer,Map<String,String>> MEMORY;
    // 硬盘信息
    public static Map<Integer,Map<String,String>> DISK;

    // 静态代码块，初始化操作系统信息
    static {
        OPERATE_SYSTEM = systemInfo.getOperatingSystem().toString();
        CPU = getCPU();
        GPU = getGPU();
        MEMORY = getMemory();
        DISK = getDisk();
        getCPU();
    }

    private static Map<String, String> getCPU() {
        Map<String, String> cpuInfo = new HashMap<>();
        CentralProcessor processor = systemInfo.getHardware().getProcessor();
        cpuInfo.put("CPU_name", processor.getProcessorIdentifier().getName().trim());
        cpuInfo.put("CPU_package", String.valueOf(processor.getPhysicalPackageCount()));
        cpuInfo.put("CPU_physical", String.valueOf(processor.getPhysicalProcessorCount()));
        cpuInfo.put("CPU_logical", String.valueOf(processor.getLogicalProcessorCount()));
        return cpuInfo;
    }

    private static Map<Integer, Map<String, String>> getGPU() {
        Map<Integer, Map<String, String>> gpuInfo = new HashMap<>();
        List<GraphicsCard> gpu = systemInfo.getHardware().getGraphicsCards();
        int i = 0;
        for (GraphicsCard card : gpu) {
            Map<String, String> cardInfo = new HashMap<>();
            cardInfo.put("GPU_name", card.getName());
            cardInfo.put("GPU_vendor", card.getVendor());
            cardInfo.put("GPU_VRAM", card.getVRam() / 1024 / 1024 + "MB");
            cardInfo.put("GPU_driver",card.getVersionInfo());
            gpuInfo.put(i++, cardInfo);
        }
        return gpuInfo;
    }

    public static Map<Integer,Map<String,String>> getMemory() {
       List<PhysicalMemory> memory = systemInfo.getHardware().getMemory().getPhysicalMemory();
       Map<Integer,Map<String,String>> memoryInfo = new HashMap<>();
       int i = 0;
       for(PhysicalMemory mem : memory){
           Map<String,String> memInfo = new HashMap<>();
           memInfo.put("mem_name",mem.getPartNumber());
           memInfo.put("mem_size",mem.getCapacity() / 1024 / 1024 + "MB");
           memInfo.put("mem_type",mem.getMemoryType());
           memInfo.put("mem_speed",mem.getClockSpeed()/1000000 + "MHz");
           memoryInfo.put(i++,memInfo);
       }
       return memoryInfo;
    }

    public static Map<Integer,Map<String,String>> getDisk(){
        Map<Integer,Map<String,String>> diskInfo = new HashMap<>();
        List<HWDiskStore> disk = systemInfo.getHardware().getDiskStores();
        int i = 0;
        for(HWDiskStore store : disk){
            Map<String,String> storeInfo = new HashMap<>();
            storeInfo.put("disk_model",store.getModel());
            storeInfo.put("disk_serial",store.getSerial());
            storeInfo.put("disk_size",store.getSize() / 1024 / 1024 + "MB");
            storeInfo.put("disk_read",store.getReadBytes() / 1024 / 1024 + "MB/s");
            storeInfo.put("disk_write",store.getWriteBytes() / 1024 / 1024 + "MB/s");
            diskInfo.put(i++,storeInfo);
        }
        return diskInfo;
    }
}
